<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/5/25  14:05
 */
namespace app\admin\Controller;
class Menu extends Common {

    //管理员表
    private $table_admin = "";

    /*
    *	构造函数
    */
    function __construct(){
        parent::__construct();
    }
    public function lists()
    {
        $menu_list = db('nav')->where('pid = 0')->select();
        $this->assign('menu_list', $menu_list);
        return $this->fetch();
    }

    //添加导航页面
    public function add_menu()
    {
        $planPath = APP_PATH.'home/Controller';
        $planList = array();
        $dirRes   = opendir($planPath);
        while($dir = readdir($dirRes))
        {
            if(!in_array($dir,array('.','..','.svn')))
            {
                $planList[] = basename($dir,'.php');
            }
        }
//        //添加子导航
//        $pid = input('pid');
//        if($pid){
//            $this->assign('pid', $pid);
//        }

        $this->assign('planList',$planList);
        return $this->fetch();
    }

    //编辑导航
    public function edit_menu()
    {
        $id = input('id');
//        $pid = input('pid');
        $name = input('name');
        $mca = input('controller').'/'.input('action');
        $number = input('number');
        $is_display = input('is_display');

//        //添加子导航
//        if($pid){
//            $data['pid'] = $pid;
//        }
        $data['name'] = $name;
        $data['mca'] = $mca;
        $data['order_number'] = $number;
        $data['is_display'] = $is_display;

        if(!$id){
            //添加
            $bool = db('nav')->insert($data);
        }else{
            //修改
            $bool = db('nav')->where('id', $id)->update($data);
        }

        if($bool){
            $this->success('操作成功！', url('Menu/lists'));
        }else{
            $this->success('操作失败！', url('Menu/lists'));
        }

        return $this->fetch();
    }

    public function del_menu()
    {
        $id = input('id');
        if($id){
            $res = db('nav')->where('id='.$id)->delete();
        }
        if($res){
            $this->success('操作成功', url('Menu/lists'));
        }else{
            $this->success('操作失败', url('Menu/lists'));
        }
    }

    function ajax_get_action()
    {
        $control = input('controller');
        $advContrl = get_class_methods("app\\home\\controller\\".str_replace('.php','',$control));
        $baseContrl = get_class_methods('app\home\controller\Base');
        $diffArray  = array_diff($advContrl,$baseContrl);
        $html = '';
        foreach ($diffArray as $val){
            $html .= "<option value='".$val."'>".$val."</option>";
        }
        exit($html);
    }
}